﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.BLL.Extend;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using System.Collections;
using Newtonsoft.Json;
namespace ZX.WebApp.EditWidows.SysManager
{
    public partial class SendAnnounce : ListPage
    {
        private string Action = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Action = Request["Action"];
            if (Action != null)
            {
                switch (Action.ToString().ToLower())
                {
                    case "load": LoadData(); break;
                    case "save": Save(); break;
                }
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void LoadData()
        {

        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            string Subject = Request["Subject"];
            string Content = Request["Content"];
            string DeptID = Guid.NewGuid().ToString();//Request["DeptID"];
            string url = Request["url"];

            BaseBussnies Bbussness = new BaseBussnies();
            //BVuseremp userMrg = new BVuseremp();
            Announce announce = new Announce();
            //V_UserEmp userInfo = userMrg.getUserInfoByUserId(this.UserId);
            
            //公告内容
            announce.ID = Guid.NewGuid();
            announce.Subject = Subject;
            announce.Content = Content;
            announce.AddUser = (Guid)this.UserId;
            announce.AddTime = DateTime.Now;
            //announce.SendDeptId = userInfo == null ? Guid.Empty : userInfo.DeptID;
            announce.AddUserName = this.User.UName;
            announce.IsOnTop = false;

            //公告接收部门
            AnnounceDepartment ad = new AnnounceDepartment();
            ad.AnnounceID = announce.ID;
            ad.DeptID = new Guid(DeptID);
            
            //添加附件
            Accessories access = new Accessories();
            access.ID = Guid.NewGuid();
            access.AccessoriesType = 1;
            access.Attach = null;
            access.Path = url;
            string[] urlSplit=url.Split('/');
            int length=urlSplit.Length;
            string name=urlSplit[length-1];
            access.Name = name;

            AnnounceAccessories annouceA = new AnnounceAccessories();
            annouceA.AnnounceID = announce.ID;
            annouceA.AttachID = access.ID;
            Bbussness.Insert(announce);
            Bbussness.Insert(ad);
            Bbussness.Insert(access);
            Bbussness.Insert(annouceA);

            string msg = "保存成功";
            msg = JsonConvert.SerializeObject(msg);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(msg);
            Response.End();
        }
    }
}
